import { app, BrowserWindow, Menu } from 'electron'

let mainWindow: BrowserWindow | null
let isDev: boolean = process.env.NODE_ENV === 'development'

const winURL = isDev
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

if (isDev) {
  require('electron-debug')()
}

function createWindow () {
  Menu.setApplicationMenu(null)
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    useContentSize: true,
    show: false
  })

  interface Extentions {
    [ key: string ]: any // Add index signature
  }

  if (isDev) {
    const exts = BrowserWindow.getExtensions() as Extentions
    if (!exts[ 'Vue.js devtools' ]) {
      BrowserWindow.addDevToolsExtension(`${__dirname}/../../.devtools/chrome`)
    }
    mainWindow.webContents.openDevTools()
  }

  mainWindow.maximize()
  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  mainWindow.once('ready-to-show', () => {
    if (mainWindow !== null) {
      mainWindow.show()
    }
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})
